package io.renren.modules.api.controller;

import io.renren.common.utils.ConstantUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.UploadUtils;
import io.renren.common.validator.Assert;
import io.renren.modules.dictionary.entity.DictionaryEntity;
import io.renren.modules.dictionary.service.DictionaryService;
import io.renren.modules.user.entity.UserinfosEntity;
import io.renren.modules.user.service.UserinfosService;
import io.renren.modules.vipuser.entity.VipinfoCustomEntity;
import io.renren.modules.vipuser.service.VipinfosService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: stf
 * @Description: 前端用户信息controller
 * @Author: 杨帅
 * @CreateDate: 2018/10/31 17:37
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/user")
public class ApiUserController {
    @Autowired
    private UserinfosService userinfosService;
    @Autowired
    private DictionaryService dictionaryService;


    /**
     * 获取用户基本信息
     */
    @GetMapping("/getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        String userInfoId = (String)request.getAttribute(ConstantUtils.USER_KEY);
        if (StringUtils.isNotBlank(userInfoId)){
            UserinfosEntity userinfosEntity = userinfosService.queryUserinfo(userInfoId);
            return R.ok().put("data",userinfosEntity);
        }
        return R.error("token获取失败");
    }

    /**
     * 获取用户积分余额
     */
    @GetMapping("/getIntegral")
    public R getIntegral(HttpServletRequest request){
        String userInfoId = (String)request.getAttribute(ConstantUtils.USER_KEY);
        if (StringUtils.isNotBlank(userInfoId)){
            UserinfosEntity userinfosEntity = userinfosService.queryApiIntegralNumber(userInfoId);
            return R.ok().put("data",userinfosEntity);
        }
        return R.error("token获取失败");
    }

    /**
     * 修改用户密码
     */
    @PostMapping("/updatePassword")
    public R updatePassword(@RequestParam String userpwd,String oldpwd,HttpServletRequest request){
        //判断密码是否符合规则
        Assert.passwordVerify(userpwd);
        String userInfoId = (String)request.getAttribute(ConstantUtils.USER_KEY);
        if (StringUtils.isNotBlank(userInfoId)){
            UserinfosEntity passwordInfo = userinfosService.queryPasswordInfo(userInfoId);
            if(!passwordInfo.getUserpwd().equals(new Sha256Hash(oldpwd, passwordInfo.getSalt()).toHex().toString())){
                return R.error("旧密码不匹配");
            }
            UserinfosEntity userinfosEntity = new UserinfosEntity();
                userinfosEntity.setUserinfoid(userInfoId);
                userinfosEntity.setUserpwd(userpwd);
                userinfosService.update(userinfosEntity);
                return R.ok("修改成功");
        }
        return R.error("token获取失败");
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/updateUserInfo")
    public R updateUserInfo(UserinfosEntity entity,HttpServletRequest request){
        Assert.isBlank(entity.getUsername(),"您的用户名不能为空");
        Assert.isBlank(entity.getPhonenum(),"您的电话号码不能为空");
        Assert.isNull(entity.getUsergender(),"您的性别不能为空");
        Assert.isBlank(entity.getAddressinfo(),"您的地址不能为空");
        //验证电话格式
        Assert.phoneVerify(entity.getPhonenum());
        //判断输入值是否符合规则
        //性别
        R usergenderError = userinfosService.judgeIsUsergenderError(entity.getUsergender());
        if (usergenderError != null){
            return usergenderError;
        }
        String userInfoId = (String)request.getAttribute(ConstantUtils.USER_KEY);
        if (StringUtils.isNotBlank(userInfoId)){
            entity.setUserinfoid(userInfoId);
            userinfosService.updateApiUser(entity);
            return R.ok("修改信息成功");
        }
        return R.error("token获取失败");
    }

    /**
     * 获取发布项目所需积分
     * @param request
     * @return
     */
    @GetMapping("/getReleaseIntegral")
    public R getReleaseIntegral(HttpServletRequest request){
        String userInfoId = (String)request.getAttribute(ConstantUtils.USER_KEY);
        if (StringUtils.isNotBlank(userInfoId)){
            UserinfosEntity userinfosEntity = userinfosService.queryObject(userInfoId);
            //根据用户身份状态获取优惠
            BigDecimal discount = getDiscount(userinfosEntity.getIsvip());
            //获取发布所需积分
            DictionaryEntity dictionaryEntity = dictionaryService.queryObjectByDateTypeName("ReleaseIntegral");
            BigDecimal releaseIntegral = new BigDecimal(dictionaryEntity.getDictionaryvalue());
            //计算优惠后所需发布积分
            BigDecimal discountIntegral = releaseIntegral.multiply(discount).setScale(2);
            Map<String,String> data = new HashMap(16);
            data.put("discount",discount.toString());
            data.put("releaseIntegral",releaseIntegral.toString());
            data.put("discountIntegral",discountIntegral.toString());
            return R.ok().put("data",data);
        }
        return R.error("token获取失败");
    }


    /**
     * 获取发布项目所需积分
     * @param request
     * @return
     */
    @GetMapping("/getBrowseIntegral")
    public R getBrowseIntegral(HttpServletRequest request){
        String userInfoId = (String)request.getAttribute(ConstantUtils.USER_KEY);
        if (StringUtils.isNotBlank(userInfoId)){
            UserinfosEntity userinfosEntity = userinfosService.queryObject(userInfoId);
            //根据用户身份状态获取优惠
            BigDecimal discount = getDiscount(userinfosEntity.getIsvip());
            //获取浏览所需积分
            DictionaryEntity dictionaryEntity = dictionaryService.queryObjectByDateTypeName("BrowseIntegral");
            BigDecimal browseIntegral = new BigDecimal(dictionaryEntity.getDictionaryvalue());
            //计算优惠后所需发布积分
            BigDecimal discountIntegral = browseIntegral.multiply(discount).setScale(2);
            Map<String,String> data = new HashMap(16);
            data.put("discount",discount.toString());
            data.put("BrowseIntegral",browseIntegral.toString());
            data.put("discountIntegral",discountIntegral.toString());
            return R.ok().put("data",data);
        }
        return R.error("token获取失败");
    }

    /**
     * 获取优惠
     * @param isVip
     * @return
     */
    private BigDecimal getDiscount(Integer isVip){
        BigDecimal divisor = new BigDecimal("10");
        if (ConstantUtils.VIP.equals(isVip)){
            DictionaryEntity vipDiscount = dictionaryService.queryObjectByDateTypeName("VIPDiscount");
            return new BigDecimal(vipDiscount.getDictionaryvalue()).divide(divisor);
        }else if(ConstantUtils.COMMON.equals(isVip)){
            DictionaryEntity discount = dictionaryService.queryObjectByDateTypeName("Discount");
            return new BigDecimal(discount.getDictionaryvalue()).divide(divisor);
        }else {
            //不优惠
            return new BigDecimal("1");
        }
    }

}
